package com.jrzh.mvc.controller.sys.admin;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jrzh.framework.I18nHelper;
import com.jrzh.framework.annotation.RequiredPermission;
import com.jrzh.framework.annotation.UserEvent;
import com.jrzh.framework.base.controller.BaseAdminController;
import com.jrzh.framework.bean.JqDataGrid;
import com.jrzh.framework.bean.ResultBean;
import com.jrzh.mvc.constants.SysActionConstants;
import com.jrzh.mvc.search.sys.MessageChannelSearch;
import com.jrzh.mvc.view.sys.MessageChannelView;
import com.jrzh.mvc.service.sys.manage.SysServiceManage;

@RestController(MessageChannelController.LOCATION +"/MessageChannelController")
@RequestMapping(MessageChannelController.LOCATION)
public class MessageChannelController extends BaseAdminController{
	public static final String LOCATION = "sys/admin/messageChannel";
	
	@Autowired
	private SysServiceManage sysServiceManage;
	
	@PostMapping(value = "datagrid")
	@RequiredPermission(key =  SysActionConstants.INDEX)
	@UserEvent(desc = "MessageChannel列表")
	public JqDataGrid<MessageChannelView> datagrid(MessageChannelSearch search) {
		JqDataGrid<MessageChannelView> dg = new JqDataGrid<MessageChannelView>();
	    try{
	    	dg = sysServiceManage.messageChannelService.datagrid(search);
	    } catch (Exception e){
	    	log.error(e);
	    }
		return dg;
	}
	
	@PostMapping(value = "add")
	@RequiredPermission(key =  SysActionConstants.ADD)
	@UserEvent(desc = "MessageChannel新增")
	public ResultBean add(MessageChannelView view){
		ResultBean result = new ResultBean();
		try{
			sysServiceManage.messageChannelService.addByView(view, getSessionUser());
			result.setStatus(ResultBean.SUCCESS);
			result.setMsg(I18nHelper.getI18nByKey("message.operation_success", getSessionUser()));
		}catch (Exception e) {
			log.error(e);
			result.setMsg(e.getMessage());
		}
		return result;	
	}
	
	@PostMapping(value = "edit/{id}")
	@RequiredPermission(key = SysActionConstants.EDIT)
	@UserEvent(desc = "MessageChannel编辑")
	public ResultBean edit(@PathVariable("id") String id, MessageChannelView view) {
		ResultBean result = new ResultBean();
		try {
			view.setId(id);
			sysServiceManage.messageChannelService.editByView(view, getSessionUser());
			result.setStatus(ResultBean.SUCCESS);
			result.setMsg(I18nHelper.getI18nByKey("message.operation_success", getSessionUser()));
		}catch (Exception e) {
			log.error(e);
			result.setMsg(e.getMessage());
		}
		return result;
	}
	
	@PostMapping(value = "delete/{id}")
	@RequiredPermission(key = SysActionConstants.DELETE)
	@UserEvent(desc = "MessageChannel删除")
	public ResultBean delete(@PathVariable("id") String id) {
		ResultBean result = new ResultBean();
		try {
			sysServiceManage.messageChannelService.delete(id, getSessionUser());
			result.setStatus(ResultBean.SUCCESS);
			result.setMsg(I18nHelper.getI18nByKey("message.operation_success", getSessionUser()));
		} catch (Exception e) {
			log.error(e);
			result.setMsg(e.getMessage());
		}
		return result;
	}
	
	@PostMapping(value = "changeStatus/{id}")
	@RequiredPermission(key = SysActionConstants.DISABLE_ENABLE)
	@UserEvent(desc = "MessageChannel启用/禁用")
	public ResultBean changeStatus(@PathVariable("id") String id, MessageChannelView view){
		ResultBean result = new ResultBean();
		try {
			view.setId(id);
			sysServiceManage.messageChannelService.editByFields(view, new String[]{"isDisable"}, getSessionUser());
			result.setStatus(ResultBean.SUCCESS);
			result.setMsg(I18nHelper.getI18nByKey("message.operation_success", getSessionUser()));
		} catch (Exception e) {
			log.error(e);
			result.setMsg(e.getMessage());
		}
		return result;
	}
	
	@Override
	protected void setData() {
		
	}

}
